<?php
    //require section
    require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
    require_once DOCUMENT_ROOT . DIR_INCLUDE . '/check_user_login.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_document.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_API . '/api_invoice.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_pager.php';
    require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . '/lib_upload.php';
    $title = 'Admin Panel';

    $id = 0;
    $email = "";
    $first_name = "";
    $last_name = "";
    $address = "";
    $phone = "";
    $file = "";
    $action = "Add";
    $action_show = "Thêm";

    if (!empty($_GET['Action']) && $_GET['Action'] === 'Edit') {
        if (!empty($_GET['Id'])) {
            $id = $_GET['Id'];
        }
        $edit_item   = api_document::get_document_by_id($id);
        $id          = $edit_item['doc_id'];
        $email       = $edit_item['doc_name'];
        $first_name  = $edit_item['doc_detail'];
        $last_name   = $edit_item['doc_cat_id'];
        $address     = $edit_item['doc_price'];
        $phone       = $edit_item['doc_img_url'];
        $file        = $edit_item['doc_file'];
        $action      = "Update";
        $action_show = "Sửa";
    } else if (!empty($_POST)) {
        $id_save = "";
        if (!empty($_POST['Id'])) {
            $id_save = $_POST['Id'];
        }
        $first_name = "";
        if (!empty($_POST['txt_name'])) {
            $first_name = $_POST['txt_name'];
        }
        $last_name = "";
        if (!empty($_POST['txt_detail'])) {
            $last_name = $_POST['txt_detail'];
        }
        $address = "";
        if (!empty($_POST['ddl_cat_id'])) {
            $address = $_POST['ddl_cat_id'];
        }

        $phone_save = "";
        if (!empty($_POST['txt_price'])) {
            $phone_save = $_POST['txt_price'];
        }
        $image_save = "";
        if (!empty($_FILES['f_image']['size'])) {
            $lib_upload = new lib_upload();
            if ($lib_upload->upload_file('f_image', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_IMAGES)) {
                $image_save = $_FILES['f_image']['name'];
            }
        } else if (!empty($_POST['hidd_image'])) {
            $image_save = $_POST['hidd_image'];
        }
        $file_save = "";
        if (!empty($_FILES['f_file']['size'])) {
            $lib_upload = new lib_upload();
            if ($lib_upload->upload_file('f_file', DOCUMENT_ROOT . DIR_SHARED_UPLOAD_DOCUMENTS)) {
                $file_save = $_FILES['f_file']['name'];
            }
        } else if (!empty($_POST['hidd_file'])) {
            $file_save = $_POST['hidd_file'];
        }

        if (!empty($_POST['Action']) && ($_POST['Action'] === "Add" || $_POST['Action'] === "Update")) {

            $error_show = api_document::validate_document_fields($first_name, $last_name, $address, $phone_save,
                $image_save, $file_save);

            if (empty($error_show)) {
                if (api_document::save_document($id_save, $first_name, $last_name, $address, $phone_save, $image_save, $file_save)) {
                    $success_info = "Thêm tài liệu &lt;" . $first_name . "&gt; thành công.";
                    if ($id_save > 0) {
                        $success_info = "Sửa tài liệu &lt;" . $first_name . "&gt; thành công.";
                    }
                }
            } else {
                $id          = $id_save;
                $email       = $first_name;
                $first_name  = $last_name;
                $last_name   = $address;
                $address     = $phone_save;
                $phone       = $image_save;
                $file        = $file_save;
                $action      = $_POST['Action'];
                $action_show = "Sửa";
                if ($action === "Add") {
                    $action_show = "Thêm";
                }
            }
        } else if (!empty($_POST['Action']) && $_POST['Action'] === "Delete") {
            $id_delete = 0;
            if (!empty($_POST['delete_id'])) {
                $id_delete = $_POST['delete_id'];
            }
            api_invoice::delete_invoice($id_delete);
            $success_info = "Xóa tài liệu thành công";
        }
    }



?>

<!DOCTYPE html>
<html>
<head>
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
    ?>
</head>
<body>
<div class="uk-container uk-container-center uk-margin-top">
    <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'menu.php';
    ?>

    <section class="ts-body-container uk-panel uk-panel-box ts-padding-large">


        <hr/>
        <h3><strong>Đơn hàng đã xử lý</strong></h3>

        <form action="?" method="POST" id="form_delete">
            <input type="hidden" name="delete_id" value="0"/>
            <input type="hidden" name="Action" value="Delete"/>
        </form>
        <form action="?" method="POST" id="form_process">
            <input type="hidden" name="process_id" value="0"/>
            <input type="hidden" name="Action" value="Process"/>
        </form>
        <table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">
            <thead>
            <tr>
                <th>STT</th>
                <th>Mã đơn hàng</th>
                <th>Khách hàng</th>
                <th>Trạng thái</th>
                <th>Tổng tiền</th>
                <th>Tổng số tài liệu</th>
                <th>Ngày tạo</th>
                <th>Thao tác</th>
            </tr>
            </thead>
            <tbody>
            <?php
                $current_page = 1;
                $page_size = 10;
                if (!empty($_GET['p'])) {
                    $current_page = $_GET['p'];
                }

                $list_total = api_invoice::get_all_paid_invoices();
                $lib_pager = new lib_pager();
                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                $count = 0;
                if (empty($current_page_list) || count($current_page_list) == 0) {
                    echo "<tr><td>chưa có đơn hàng nào được xử lý</td></td>";
                } else {
                    foreach ($current_page_list as $i => $item) {
                        ?>
                        <tr>
                            <td><?php echo $i + 1; ?></td>
                            <td><?php echo $item['invoice_id'] ?></td>
                            <td><?php echo $item['email'] ?></td>
                            <td><?php echo $item['inv_status_name'] ?></td>
                            <td><?php echo number_format($item['total_money'],0,'.',',') ?></td>
                            <td><?php echo $item['total_quantity'] ?></td>
                            <?php
                            $date = date_create($item["created_date"]);
                            $date_str = date_format($date, "d/m/Y");
                            ?>
                            <td><?php echo $date_str; ?></td>
                            <td>
                                <a href="invoice_detail.php?invoice_id=<?php echo $item['invoice_id']; ?>"><i
                                        class="uk-icon-search"></i></a>
                            </td>
                        </tr>
                    <?php
                    }
                } ?>
            <tr align="right" style="color:Black;background-color:#F7F7DE;">
                <td colspan="8">
                    <table>
                        <tbody>
                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                            <td colspan="7">
                                <table>
                                    <tbody>
                                    <tr>
                                        <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                            <td>
                                                                                <span>
                                                                                    <a href="?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                                </span>
                                            </td>
                                        <?php } ?>
                                    </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            </tbody>
        </table>
    </section>

    <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
    ?>
</div>
</body>
</html>
